package com.google.code.p.ivans;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.sql.SQLException;
import java.util.GregorianCalendar;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.p.ivans.entities.Waiter;
import com.google.code.p.ivans.serviceinterfaces.WaiterServiceInterface;
import com.google.code.p.ivans.serviceinterfacesimpls.WaiterServiceInterfaceImpl;


public class AddWaiter extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		WaiterServiceInterface waiterSI = new WaiterServiceInterfaceImpl();		
		
		try {			
			
			String firstName = new String(request.getParameter("firstName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			String lastName = new String(request.getParameter("lastName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			String patronymicName = new String(request.getParameter("patronymicName").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			int day = Integer.valueOf(request.getParameter("day"));
			
			String monthStr = new String(request.getParameter("month").getBytes(StandardCharsets.ISO_8859_1), StandardCharsets.UTF_8);
			
			int year  = Integer.valueOf(request.getParameter("year"));
			
			int salary = Integer.valueOf(request.getParameter("salary"));
			
			if(firstName.equals("")) {
				request.setAttribute("error", "Введите имя");
			}			
			else if(lastName.equals("")) {
				request.setAttribute("error", "Введите фамилию");
			}
			else if(patronymicName.equals("")) {
				request.setAttribute("error", "Введите отчество");
			}
			else if(salary < 0) {
				request.setAttribute("error", "Введите допустимую зарплату");
			}
			else {
				int month = 0;
				switch(monthStr) {
					case "январь": month = 0; break;
					case "февраль": month = 1; break;
					case "март": month = 2; break;
					case "апрель": month = 3; break;
					case "май": month = 4; break;
					case "июнь": month = 5; break;
					case "июль": month = 6; break;
					case "август": month = 7; break;
					case "сентябрь": month = 8; break;
					case "октябрь": month = 9; break;
					case "ноябрь": month = 10; break;
					case "декабрь": month = 11; break;
				}
				waiterSI.addWaiter(new Waiter(firstName, lastName, patronymicName, 
											new GregorianCalendar(year, month, day), 
											new GregorianCalendar(), 
											salary));			
				
				RequestDispatcher requestDispatcher = request.getRequestDispatcher("successfulWaiterAdding.jsp");
				
				requestDispatcher.forward(request, response);
			}	
			
		}
		catch(Exception e) {
			RequestDispatcher requestDispatcher = request.getRequestDispatcher("errorWaiterAdding.jsp");
			
			requestDispatcher.forward(request, response);
		}
	}

}
